home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / viewports / viewport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.0 KB  |  167 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* viewport.c - Demonstrates how to separate a window into two drawing 
  19.  *    areas using glViewport().  It creates two viewports that divide 
  20.  *    the window in half. In each half, a rectangle the size of the 
  21.  *    viewing volume is rendered. In one half it also renders a square 
  22.  *    grid. Note how the mapping of the viewing volume to the viewport
  23.  *    affects the shape of the grid.
  24.  *
  25.  *    SPACE key    - toggle which viewport the grid is drawn in
  26.  *    Escape Key    - exit program
  27.  */
  28. #include <GL/gl.h>
  29. #include <GL/glu.h>
  30. #include <GL/glut.h>
  31.  
  32. #include <stdio.h>
  33.  
  34. /*  Function Prototypes  */
  35.  
  36. GLvoid  initgfx( GLvoid );
  37. GLvoid  keyboard( GLubyte, GLint, GLint );
  38. GLvoid  drawScene( GLvoid );
  39.  
  40. void printHelp( char * );
  41.  
  42. /* Global Definitions */
  43.  
  44. #define KEY_ESC    27    /* ascii value for the escape key */
  45.  
  46. /* Global Variables */
  47.  
  48. static    GLboolean    grid_in_bottom = GL_TRUE;
  49.  
  50. GLvoid
  51. main( int argc, char *argv[] )
  52. {
  53.     GLsizei width, height;
  54.  
  55.     glutInit( &argc, argv );
  56.  
  57.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  58.     height = glutGet( GLUT_SCREEN_HEIGHT );
  59.     glutInitWindowPosition( width / 4, height / 4 );
  60.     glutInitWindowSize( width / 2, height / 2 );
  61.     glutInitDisplayMode( GLUT_RGBA );
  62.     glutCreateWindow( argv[0] );
  63.  
  64.     initgfx();
  65.  
  66.     glutKeyboardFunc( keyboard );
  67.     glutDisplayFunc( drawScene ); 
  68.  
  69.     printHelp( argv[0] );
  70.  
  71.     glMatrixMode( GL_PROJECTION );
  72.     glOrtho( -2.0, 2.0, -2.0, 2.0, -1.0, 1.0 );
  73.     glMatrixMode( GL_MODELVIEW );
  74.  
  75.     glutMainLoop();
  76. }
  77.  
  78. void
  79. printHelp( char *progname )
  80. {
  81.     fprintf(stdout, "\n%s - demonstrates how the viewport controls \n"
  82.         "what portion of the window will be rendered into\n\n"
  83.         "SPACE key        - toggle which viewport the grid is drawn in\n"
  84.         "Escape Key        - exit the program\n\n",
  85.         progname);
  86. }
  87.  
  88. GLvoid
  89. initgfx( GLvoid )
  90. {
  91.     glClearColor( 1.0, 0.0, 1.0, 1.0 ); /* magenta */
  92.     glShadeModel( GL_FLAT );
  93. }
  94.  
  95. GLvoid 
  96. keyboard( GLubyte key, GLint x, GLint y )
  97. {
  98.     switch (key) {
  99.     case ' ':    /* toggle grid location */
  100.         grid_in_bottom = !grid_in_bottom;
  101.         glutPostRedisplay();
  102.         break;
  103.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  104.         exit(0);
  105.     }
  106. }
  107.  
  108. /* Draw a square grid */
  109. GLvoid
  110. drawSquareGrid( GLvoid )
  111. {
  112.     GLfloat        x, y;
  113.  
  114.     glBegin( GL_LINES );
  115.     for ( x = -1.0; x < 1.1; x += 0.1 ) {
  116.         glVertex2f( x, -1.0 );
  117.         glVertex2f( x, 1.0 );
  118.     }
  119.     for ( y = -1.0; y < 1.1; y += 0.1 ) {
  120.         glVertex2f( -1.0, y );
  121.         glVertex2f( 1.0, y );
  122.     }
  123.     glEnd();
  124. }
  125.  
  126.  
  127. GLvoid
  128. drawScene( GLvoid )
  129. {
  130.     GLsizei        width, height;
  131.  
  132.     static GLfloat  blueColor[] = { 0.0, 0.0, 1.0 };
  133.     static GLfloat  greenColor[] = { 0.0, 1.0, 0.0 };
  134.  
  135.     glClear( GL_COLOR_BUFFER_BIT );
  136.  
  137.     /* Get the current window size */
  138.     width = glutGet(GLUT_WINDOW_WIDTH); 
  139.     height = glutGet(GLUT_WINDOW_HEIGHT);
  140.  
  141.     /* Create the first viewport - the bottom half of the screen */
  142.     glViewport( 0, 0, width, height / 2.0 );
  143.  
  144.     /* Draw a blue rectangle that fills the entire viewing volume */
  145.     glColor3fv( blueColor );
  146.     glRectf( -2.0, -2.0, 2.0, 2.0 );
  147.  
  148.     if (grid_in_bottom) {
  149.         glColor3f( 1.0, 1.0, 1.0 );
  150.         drawSquareGrid();
  151.     }
  152.  
  153.     /* Create the second viewport - the top half of the screen */
  154.     glViewport( 0, height / 2.0, width, height / 2.0 );
  155.  
  156.     /* Draw a green rectangle that fills the entire viewing volume */
  157.     glColor3fv( greenColor );
  158.     glRectf( -2.0, -2.0, 2.0, 2.0 );
  159.  
  160.     if (!grid_in_bottom) {
  161.         glColor3f( 0.0, 0.0, 0.0 );
  162.         drawSquareGrid();
  163.     }
  164.  
  165.     glFlush();
  166. }
  167.